<?php
class RegisterAction extends Action {
	private $emailKey = 'sfw3odmn109sqppq';
	// 注册页面显示
	public function index() {
		$this->display ();
	}
	
	// 注册
	public function reg() {
		if (session ( 'verify' ) == md5 ( $this->_post ( 'verify' ) )) {
			$User = D ( 'User' );
			if (! $User->create ()) {
				$this->error ( $User->getError () );
			} else {
				if (! empty ( $_POST ['user_invited'] ))
					$User->user_invited = base64_decode ( $this->_post ( 'user_invited' ) );
				$User->user_real_password = $this->_post ( 'user_password' );
				$user_id = $User->add ();
				if ($user_id) {
					$this->sendEmail ( $user_id, $this->_post ( 'user_email' ) );
					$this->assign ( 'waitSecond', 5 );
					$this->success ( '注册成功！验证已发送到您注册邮箱，请点击邮箱中的链接完成邮箱验证！<br>（如未收到邮件请在垃圾邮箱中查看！）', '/' );
				}
			}
		} else {
			$this->error ( '验证码输入错误！' );
		}
	}
	private function sendEmail($user_id, $email) {
		vendor ( 'PHPMailer.phpmailer' );
		$mail = new PHPMailer ();
		$mail->IsSMTP ();
		
		//$rand = mt_rand(1,3);
		//if($rand == 1)
		//	{
		//	$mail->Host = 'smtp.exmail.qq.com';
		//	}else if($rand == 2){
		//	$mail->Host = 'smtp.exmail.qq.com';
		//	}else{
		//		$mail->Host = 'smtp.exmail.qq.com';
		//	}
		//$mail->Host = 'smtp.ym.163.com';
		//$mail->Host = 'mail6.myhostadmin.net';
		//$mail->Host = 'smtp.exmail.qq.com';
		//$mail->Host = 'smtp.exmail.sina.com';
		
		/*
		$mail->Host = M('Config')->getFieldByConfigName('emailSmtp','config_value');
		$mail->Username = 'service@addindex.com';
		$mail->Password = 'wodeyuan1989';
		$mail->From = 'service@addindex.com';
		*/

		$mail->Host = 'smtp.163.com';
		$mail->Username = 'addindex@163.com';
		$mail->Password = 'wodeyuan1989';
		$mail->From = 'addindex@163.com';


		$mail->SMTPAuth = true;		
		$mail->FromName = 'Addindex';
		$mail->AddAddress ( $email );
		$mail->IsHTML ( true );
		$mail->CharSet = "utf-8";
		$mail->Subject = "来自百度指数公开版的邮箱验证";
		$url = 'http://' . $_SERVER ['HTTP_HOST'] . '/Register/verifyEmail/?key=' . base64_encode ( $user_id . '+' . md5 ( $user_id . $email . $this->emailKey ) );
		$html = '<html><head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>';
		$html .= '<div style="border:1px solid #ccc;padding: 15px 15px 10px 15px;border-radius: 3px;">';
		$html .= '<p>您好：</p><p>欢迎加入百度指数公开版（www.addindex.com）！！</p><p>请 <a href="' . $url . '" target="_blank" style="color:#1887E8">点击这里</a> 完成邮箱验证</p>';
		$html .= '<p>安全提示：<br>为了您的账户安全，我们建议您点击以下链接验证邮箱：<br><a href="'.$url.'" target="_blank" style="color:#1887E8">'.$url.'</a><br>您也可以将链接复制到浏览器地址栏访问<br>谢谢</p>';
		$html .= '</div></body></html> ';
		$mail->Body = $html;
		if (! $mail->Send ()) {
			echo "Message could not be sent. <p>";
			echo "Mailer Error: " . $mail->ErrorInfo;
			exit ();
		}
	}
	
	// 验证邮箱
	public function verifyEmail() {
		$key = $this->_get ( 'key' );
		$info = explode ( '+', base64_decode ( $key ) );
		$User = M ( 'User' );
		$userInfo = $User->find ( $info [0] );
		if ($userInfo ['user_email_verify'] == 1) {
			$this->error ( '邮箱已经验证成功！请勿重复验证', '/' );
		} else {
			if ($info [1] == md5 ( $userInfo ['user_id'] . $userInfo ['user_email'] . $this->emailKey )) {
				if ($User->where ( 'user_id = ' . $userInfo ['user_id'] )->setField ( 'user_email_verify', 1 )) {
					$userInfo ['user_email_verify'] = 1;
					session ( 'user', $userInfo );
					cookie ( 'userCookie[' . $userInfo ['user_id'] . ']', $_SESSION ['user'] ['user_last_loginip'], 3600 * 24 * 365 );
					if ($_SESSION ['user'] ['user_invited'] > 0) {
						$userInfo = $User->find ( $_SESSION ['user'] ['user_invited'] );
						$userCookie = cookie ( 'userCookie' );
						if ($userInfo ['user_register_ip'] != $_SESSION ['user'] ['user_register_ip'] && $userInfo ['user_last_loginip'] != $_SESSION ['user'] ['user_register_ip'] && $userInfo ['user_password'] != $_SESSION ['user'] ['user_password'] && ! isset ( $userCookie [$_SESSION ['user'] ['user_invited']] ))
							$User->where ( 'user_id = ' . $_SESSION ['user'] ['user_invited'] )->setInc ( 'user_add_times', 10 );
					}
					$this->success ( '验证成功！', '/' );
				}
			} else {
				$this->error ( '异常错误！', '/' );
			}
		}
	}
	
	// 验证码
	public function verify() {
		import ( 'ORG.Util.Image' );
		Image::buildImageVerify ( 4, 1, 'png', 63, 20 );
	}
}